package com.zjy.health_ms.controller;

import com.zjy.health_ms.entity.Result;
import com.zjy.health_ms.pojo.User;
import com.zjy.health_ms.service.UserService;
import com.zjy.health_ms.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: health_ms
 * @ClassName UserController
 * @description:
 * @author: Ats
 * @create: 2025-05-12 11:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public Result login(@RequestBody User user){
        System.out.println(user.getUsername());
        user = userService.login(user.getUsername(), user.getPassword());
        if (user !=null ){
            //生成一个token（加密后的user信息）
            String token = JWTUtil.generateJWT(user);
            return Result.buildSuccess(token);
        }
        return Result.buildFail();
    }

}
